package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.PayMethodAdapter;
import com.jiayang.hhotel.bean.CheckOutMethodModel;
import com.jiayang.hhotel.bean.CheckOutModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.fragment.CheckOutTabFragment;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 结账退房子页面逻辑类
 */

public class CheckOutTabPresenter extends XPresent<CheckOutTabFragment> {

    /**
     * 获取结账列表
     * @param mpLanguage 语言
     * @param mpOperation --0当前、1-历史
     * @param mpGstacct 客户账号（个人 团队都用这个）
     * @param mpType 1 自付账单 4 团会账单
     */
    public void asyncListLoad(int mpLanguage,int mpOperation,long mpGstacct,int mpType){
        Api.getHomeSer().checkOut(Constant.PHONE,mpLanguage,mpOperation,mpGstacct,mpType)
                .compose(XApi.<CheckOutModel>getApiTransformer())
                .compose(XApi.<CheckOutModel>getScheduler())
                .compose(getV().<CheckOutModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<CheckOutModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().sealPic(error.getType());
                    }

                    @Override
                    public void onNext(CheckOutModel Model) {
                        LogUtil.showLog("结账退房数据类="+Model.toString());
                        switch (Model.getState()) {
                            case 9://请求成功
                                getV().setAdapter(Model.getPTransDetailListDtos());
                                break;
                            case -1://失败
                                getV().sealPic(-1);
                                break;
                            case 1002://没有数据
                                getV().sealPic(1002);
                                break;
                        }
                    }
                });
    }

    /**
     * 获取结账方式列表
     * @param mpLanguage 语言
     * @param mpType 1-酒店结账
     * @param mpRegister 结账付款金额<0时使用(默认-1）
     * @param mpGstAcct 结账付款金额<0时使用(默认-1）
     * @param mpMark 结账付款金额<0时使用(默认-1）0-自付结账、1-公付结账
     * @param mpAmount 金额
     */
    public void asyncCheckOutMethodListLoad(int mpLanguage,int mpType,long mpRegister,long mpGstAcct,int mpMark,double mpAmount){
        Api.getHomeSer().checkOutMethod(Constant.PHONE,mpLanguage,mpType,mpRegister,mpGstAcct,mpMark,mpAmount)
                .compose(XApi.<CheckOutMethodModel>getApiTransformer())
                .compose(XApi.<CheckOutMethodModel>getScheduler())
                .compose(getV().<CheckOutMethodModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<CheckOutMethodModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV().getActivity());
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV().getActivity());
                    }

                    @Override
                    public void onNext(CheckOutMethodModel Model) {
                        CommonUtil.dismissProgressDialog(getV().getActivity());
                        switch (Model.getState()){
                            case 9://成功
                                getV().initCheckOutMethodDialog(Model.getPCashierPayListDtos());
                                break;
                            case -1://失败
                                ToastUtil.Toast(getV().getString(R.string.error_code)+Model.getState()+getV().getString(R.string.space)+getV().getString(R.string.service_error_contract_manager),getV().getActivity());
                                break;
                        }
                    }
                });
    }

    /**
     * 保存结账方式
     * @param mpLanguage 语言
     * @param mpType 1 自付账单 2 团会账单
     * @param mpGstacct 客户账户
     * @param mpTrncode 结账方式代码
     * @param mpAmount 金额
     * @param mpSerial 选择的账项代码拼接成字符串
     */
    public void asyncSaveCheckOutMethodListLoad(int mpLanguage,int mpType,long mpGstacct,int mpTrncode,double mpAmount,String mpSerial){
        Api.getHomeSer().saveCheckOutMethod(Constant.PHONE,mpLanguage,mpType,mpGstacct,mpTrncode,mpAmount,mpSerial)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV().getActivity());
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV().getActivity());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        CommonUtil.dismissProgressDialog(getV().getActivity());
                        switch (Model.getState()){
                            case 9://成功
                                checkOutMethodDialog.dismiss();
                                ToastUtil.Toast(getV().getString(R.string.already_submit),getV().getActivity());
                                getV().isRefresh(true);
                                getV().getCheckOutList();
                                break;
                            case -1://失败
                                ToastUtil.Toast(getV().getString(R.string.error_code)+Model.getState()+getV().getString(R.string.space)+getV().getString(R.string.service_error_contract_manager),getV().getActivity());
                                break;
                        }
                    }
                });
    }

    private AlertDialog checkOutMethodDialog;
    /**
     * 结账方式对话框
     * @param context 上下文
     * @param list 集合
     */
    public void checkOutMethodDialog(Context context, final List<CheckOutMethodModel.PCashierPayListDtosBean> list){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView= LayoutInflater.from(context).inflate(R.layout.dialog_pay_method,null);
        TextView textView= (TextView) itemView.findViewById(R.id.tv_title);
        Button enterMine= (Button) itemView.findViewById(R.id.bt_determine);
        textView.setText(context.getString(R.string.zhifu_method));
        ListView listView= (ListView) itemView.findViewById(R.id.lv);
        final PayMethodAdapter adapter=new PayMethodAdapter(context,list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setIndex(position);
            }
        });
        enterMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getV().saveCheckOutMethod(list.get(adapter.getIndex()));
            }
        });
        builder.setView(itemView);
        checkOutMethodDialog =builder.show();
    }
}
